/*
 * ScopeChain.java - ScriptME
 * 
 * Copyright (c) 2009 Cesar Henriques <cesar at alttab.com.ar>.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Based on FESI Project
 * 
 * Contributors:
 * 	Jean-Marc Lugrin - initial API and implementation
 * 	Cesar Henriques <cesar at alttab.com.ar> - J2ME Porting and Extensions
 */
package org.scriptme.ecmascript.interpreter;

import org.scriptme.ecmascript.data.ESObject;
import org.scriptme.ecmascript.data.ESReference;
import org.scriptme.ecmascript.data.ESValue;
import org.scriptme.ecmascript.exceptions.EcmaScriptException;

/**
 * The scope chaine has established by WITH statements. Used to lookup values by
 * name.
 */
public class ScopeChain {

	/** The previous element. */
	private ScopeChain previousElement;

	/** The this element. */
	private ESObject thisElement;

	/**
	 * CReate a new scope chain linked to a previous one (which is null only for
	 * the topmost chain).
	 * 
	 * @param thisElement
	 *            Object to look at at this level
	 * @param previousElement
	 *            previous object in scope chain
	 */
	ScopeChain(ESObject thisElement, ScopeChain previousElement) {
		this.previousElement = previousElement;
		this.thisElement = thisElement;
	}

	/**
	 * Return the previous element in scope chain.
	 * 
	 * @return The previous element
	 */
	ScopeChain previousScope() {
		return previousElement;
	}

	/**
	 * Return a reference to an object in the scope chain, so that the value can
	 * be accessed or modified.
	 * 
	 * @param identifier
	 *            the identifier
	 * 
	 * @return an ESReference object
	 * 
	 * @throws EcmaScriptException
	 *             the ecma script exception
	 * 
	 * @exception EcmaScriptException
	 *                Not thrown
	 */
	public ESReference getReference(String identifier)
			throws EcmaScriptException {
		return getReference(identifier, identifier.hashCode());
	}

	/**
	 * Return a reference to an object in the scope chain, so that the value can
	 * be accessed or modified.
	 * 
	 * @param identifier
	 *            the identifier
	 * @param hash
	 *            the hash
	 * 
	 * @return an ESReference object
	 * 
	 * @throws EcmaScriptException
	 *             the ecma script exception
	 * 
	 * @exception EcmaScriptException
	 *                Not thrown
	 */
	public ESReference getReference(String identifier, int hash)
			throws EcmaScriptException {
		ScopeChain theChain = this;
		do {
			if (theChain.thisElement.hasProperty(identifier, hash)) {
				return new ESReference(theChain.thisElement, identifier, hash);
			}
			theChain = theChain.previousElement;
		} while (theChain != null);
		return new ESReference(null, identifier, hash);
	}

	/**
	 * Return a value for an object in the scope chain. return ESUndefined for
	 * undefined properties of existing objects, but generate an error for
	 * unexistent object.
	 * <P>
	 * A variant of getProperty is used, which will call recursively this
	 * routine and the getProperty in a previous scope until all scopes have
	 * been examined or a value is returned. This avoid the call of hasProperty
	 * followed by a second call to getProperty to get the property value.
	 * 
	 * @param identifier
	 *            the identifier
	 * 
	 * @return The value of the object, possibly ESUndefined
	 * 
	 * @throws EcmaScriptException
	 *             the ecma script exception
	 * 
	 * @exception EcmaScriptException
	 *                if no global object has that value
	 */
	public ESValue getValue(String identifier) throws EcmaScriptException {
		return getValue(identifier, identifier.hashCode());
	}

	/**
	 * Return a value for an object in the scope chain. return ESUndefined for
	 * undefined properties of existing objects, but generate an error for
	 * unexistent object.
	 * <P>
	 * A variant of getProperty is used, which will call recursively this
	 * routine and the getProperty in a previous scope until all scopes have
	 * been examined or a value is returned. This avoid the call of hasProperty
	 * followed by a second call to getProperty to get the property value.
	 * 
	 * @param hash
	 *            The hash code of the element (for optimization)
	 * @param identifier
	 *            the identifier
	 * 
	 * @return The value of the object, possibly ESUndefined
	 * 
	 * @throws EcmaScriptException
	 *             the ecma script exception
	 * 
	 * @exception EcmaScriptException
	 *                if no global object has that value
	 */
	public ESValue getValue(String identifier, int hash)
			throws EcmaScriptException {
		return thisElement
				.getPropertyInScope(identifier, previousElement, hash);
	}

	/**
	 * Call a function defined by name in the scope chain.
	 * <P>
	 * A variant of doCall is used, which will call recursively this routine and
	 * the doCall in a previous scope until all scopes have been examined or a
	 * value is returned. This avoid the call of hasProperty followed by a
	 * second call to getProperty to get the property value.
	 * 
	 * @param hash
	 *            The hash code of the function name (for optimization)
	 * @param arguments
	 *            The parameters of the call
	 * @param evaluator
	 *            the evaluator
	 * @param thisObject
	 *            the this object
	 * @param functionName
	 *            the function name
	 * 
	 * @return The result of the call, possibly NULL
	 * 
	 * @throws EcmaScriptException
	 *             the ecma script exception
	 * 
	 * @exception EcmaScriptException
	 *                for any error
	 */
	public ESValue doIndirectCall(Evaluator evaluator, ESObject thisObject,
			String functionName, int hash, ESValue[] arguments)
			throws EcmaScriptException {
		return thisElement.doIndirectCallInScope(evaluator, previousElement,
				thisObject, functionName, hash, arguments);
	}
}
